<html>
<head>
<link rel="shortcut icon" href="./favicon.ico">
<link rel="stylesheet" type="text/css" href="./style.css">
<link rel="canonical" href="./Bitmask_1_Bit_at_Rightmost_0_Bit.html">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Credit: [Hacker's Delight](./reading.html#Warren2013), Section 2-1: Manipulating Rightmost Bits">
<title>Bitmask 1 Bit at Rightmost 0 Bit</title>
</head>
<body>

<p class="inline bordered"><b><a href="./Bitmask_1_Bit_at_Rightmost_0_Bit.v">Source</a></b></p>
<p class="inline bordered"><b><a href="./legal.html">License</a></b></p>
<p class="inline bordered"><b><a href="./index.html">Index</a></b></p>

<h1>Bitmask: 1 Bit at Rightmost 0 Bit</h1>
<p>Credit: <a href="./reading.html#Warren2013">Hacker's Delight</a>, Section 2-1: Manipulating Rightmost Bits</p>
<p>Use the following formula to create a word with a single 1-bit at the
 position of the rightmost 0-bit in the input, producing 0 if none (e.g.,
 10100111 -&gt; 00001000)</p>

<pre>
`default_nettype none

module <a href="./Bitmask_1_Bit_at_Rightmost_0_Bit.html">Bitmask_1_Bit_at_Rightmost_0_Bit</a>
#(
    parameter WORD_WIDTH = 0
)
(
    input   wire    [WORD_WIDTH-1:0]    word_in,
    output  reg     [WORD_WIDTH-1:0]    word_out
);

    initial begin
        word_out = {WORD_WIDTH{1'b0}};
    end

    localparam ONE = {{WORD_WIDTH-1{1'b0}},1'b1};

    always @(*) begin
        word_out = ~word_in & (word_in + ONE);
    end

endmodule
</pre>

<hr>
<p><a href="./index.html">Back to FPGA Design Elements</a>
<center><a href="https://fpgacpu.ca/">fpgacpu.ca</a></center>
</body>
</html>

